package cz.muni.fi.pa165.soccergamesarchive.rest.services;

import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.TeamDTO;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.services.TeamServiceBeanLocal;
import java.util.List;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Rest Service for Teams
 *
 * @author Tomas Plevko
 */
@Stateless
@Path(value = "team")
@DeclareRoles("Archiver")
public class TeamRestService {

    @EJB
    private TeamServiceBeanLocal teamService;

    public TeamRestService() {
    }

    @POST
    @Path("new")
    @Consumes(MediaType.TEXT_XML)
    @Produces(MediaType.TEXT_XML)
    @RolesAllowed("Archiver")
    public TeamDTO addTeam(TeamDTO team) {

        return teamService.addTeam(team);
    }

    @POST
    @Path("change")
    @Consumes(MediaType.TEXT_XML)
    @Produces(MediaType.TEXT_XML)
    @RolesAllowed("Archiver")
    public TeamDTO changeTeam(TeamDTO team) throws RuntimeException {

        return teamService.changeTeam(team);
    }

    @POST
    @Path("delete")
    @Consumes(MediaType.TEXT_XML)
    @RolesAllowed("Archiver")
    public void removeTeam(TeamDTO team) throws RuntimeException {

        teamService.removeTeam(team);
    }

    @GET
    @Path("getall")
    @Produces(MediaType.TEXT_XML)
    public List<TeamDTO> getAllTeams() {

        return teamService.getAllTeams();
    }

    @GET
    @Path("getlimnumber/{offset}/{limit}")
    @Produces(MediaType.TEXT_XML)
    @Consumes(MediaType.TEXT_XML)
    public List<TeamDTO> getLimitedNumberOfTeams(@PathParam("offset") int offset,
                                                 @PathParam("limit") int limit)
            throws RuntimeException {

        return teamService.getLimitedNumberOfTeams(offset, limit, null, null);
    }

    @GET
    @Path("id/{teamId}")
    @Produces(MediaType.TEXT_XML)
    public TeamDTO getTeamById(@PathParam("teamId") Long teamId) throws
            RuntimeException {

        return teamService.getTeamById(teamId);
    }

    @GET
    @Path("count")
    @Consumes(MediaType.TEXT_XML)
    @Produces(MediaType.TEXT_PLAIN)
    public int getFilteredTeamsCount() throws RuntimeException {

        return teamService.getFilteredTeamsCount(null);
    }

    @POST
    @Path("availnumbers")
    @Consumes(MediaType.TEXT_XML)
    @Produces(MediaType.TEXT_XML)
    @RolesAllowed("Archiver")
    public List<Integer> getAvaliableShirtNumbers(TeamDTO team) throws
            RuntimeException {

        return teamService.getAvaliableShirtNumbers(team);
    }

    @GET
    @Path("find/{teamName}")
    @Produces(MediaType.TEXT_XML)
    public List<TeamDTO> findTeam(@PathParam("teamName") String teamName) throws
            RuntimeException {

        return teamService.findTeam(teamName);
    }

}
